//
//  MovingTabBarVC.swift
//  TT_Swift
//
//  Created by Lenny on 16/5/11.
//  Copyright © 2016年 Lenny. All rights reserved.
//

import UIKit

class MovingTabBarVC: UITabBarController {
    
    var _allBtns:NSMutableArray?
    var tabBarView:UIView?
    var selectBtn:UIButton?
    
    //MARK:life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.redColor()
        
        //准备数据
        setData()
        //设置tabbarvcs
        setTabBar()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    //MARK:private motheds
    func setData() {
        
        _allBtns = NSMutableArray()
    }
    
    func setTabBar() {
        
        let v1:ViewController = ViewController()
        let v2:ViewController = ViewController()
        let v3:ViewController = ViewController()
        let v4:ViewController = ViewController()
        let v5:ViewController = ViewController()
        
        self.viewControllers = [v1,v2,v3,v4,v5]
        let titles = ["公放","留言","熄火","续航","做伴"]
        for var item:UITabBarItem in self.tabBar.items! {
            
            let index = self.tabBar.items!.indexOf(item)!
            item.title = titles[index]
        }
        self.tabBar.hidden = true
        
        //准备UI视图
        proUI()
        self.view.addSubview(tabBarView!)
    }
    
    func proUI() {
        
        tabBarView = UIView(frame: CGRect(x: 0, y: k_screen_h - k_M_Tabbar_h, width: k_screen_w, height: k_M_Tabbar_h))
        tabBarView!.backgroundColor = WL_M_Bar_Back_Color
        
        let num = self.tabBar.items?.count
        let wide:CGFloat = k_screen_w/CGFloat(num!)
        for var i = 0; i < num; i++ {
            
            let item = self.tabBar.items?[i]
            
            let btn = UIButton(frame: CGRect(x: wide * CGFloat(i), y: 0, width: wide, height: k_M_Tabbar_h))
            tabBarView?.addSubview(btn)
            _allBtns?.addObject(btn)
            btn.tag = i
            
            //设置初始状态
            if i == 0 {
                
                btn.selected = true
                selectBtn = btn
            }
            //设置按键事件
            if i != 1 {
                
                btn.addTarget(self, action:"btnAction:", forControlEvents: UIControlEvents.TouchUpInside)
            }else {
                
                let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressGesture:")
                longPress.minimumPressDuration = 0.1
                btn.addGestureRecognizer(longPress)
            }
  
            let image = UIImage(named: "icon_tabbar_m_\(i)_normal")
            let selectImage = UIImage(named: "icon_tabbar_m_\(i)_select")
            let imageSize = image?.size
            if i == 2 {
                
                btn.frame = CGRect(x: btn.frame.origin.x, y: -5, width: wide, height: k_M_Tabbar_h + 5)
                btn.backgroundColor = WL_M_Bar_H_Color
            }
            //添加按键上图片
            let btnImageV = UIImageView()
            btnImageV.image = image
            btnImageV.highlightedImage = selectImage
            btn.addSubview(btnImageV)
            //添加按键上文字
            let nameL = UILabel()
            btn.addSubview(nameL)
            nameL.text = item!.title
            nameL.textColor = UIColor.whiteColor()
            nameL.font = UIFont.systemFontOfSize(13)
            
            //使用SnapKit自动布局
            btnImageV.snp_makeConstraints(closure: { (make) -> Void in
                make.size.equalTo(imageSize!)
                make.centerY.equalTo(btn).offset(-10)
                make.centerX.equalTo(btn)
            })
            nameL.snp_makeConstraints(closure: { (make) -> Void in
                make.top.equalTo(btnImageV.snp_bottom).offset(3)
                make.centerX.equalTo(btn)
            })
            refreshStateWithBtn(btn)
        }
    }
    
    func refreshStateWithBtn(sender:UIButton) {
        let imgV:UIImageView = sender.subviews[0] as! UIImageView
        let nameL:UILabel = sender.subviews[1] as! UILabel
        if sender.selected {
            
            nameL.textColor = WL_M_Bar_H_Color
            imgV.highlighted = true
        }else {
            nameL.textColor = UIColor.whiteColor()
            imgV.highlighted = false
        }
    }
}
